package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LableController {

    @Autowired
    private LabelService labelService;

    //查询所有
    @RequestMapping(method = RequestMethod.GET)
    public Result<Label> findAll() {
        return new Result<Label>(true, StatusCode.OK, "查询成功", labelService.findAll());
    }

    //根据ID查询
    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result<Label> findById(@PathVariable String labelId) {
        int i = 1 / 10;//借助BaseExceptionHandler把他处理成Result
        return new Result<Label>(true, StatusCode.OK, "查询成功", labelService.findById(labelId));
    }

    //添加
    @RequestMapping(method = RequestMethod.POST)
    public Result<Label> save(@RequestBody Label label) {
        labelService.add(label);
        return new Result<Label>(true, StatusCode.OK, "添加成功");
    }

    //修改
    @RequestMapping(value = "/{labelId}", method = RequestMethod.PUT)
    public Result<Label> update(@PathVariable String labelId, @RequestBody Label label) {
        label.setId(labelId);
        labelService.update(label);
        return new Result<Label>(true, StatusCode.OK, "更新成功");
    }

    //删除
    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public Result<Label> deleteById(@PathVariable String labelId) {
        labelService.deleteById(labelId);
        return new Result<Label>(true, StatusCode.OK, "删除成功");
    }

    //根据条件查询
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result<Label> findSearch(@RequestBody Map map) {
        List<Label> list = labelService.findSearch(map);
        return new Result<Label>(true, StatusCode.OK, "搜索查询成功", list);
    }



    //根据分页查询
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result pageQuery(@RequestBody Map map, @PathVariable int page, @PathVariable int size) {
        Page<Label> pageList = labelService.pageQuery(map,page,size);
        return new Result<Label>(true, StatusCode.OK, "搜索查询成功", new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

}
